<?php

namespace app\api\controller\mall;

use app\common\controller\Api;
use app\common\model\mall\GoodsEvaluate;

class Evaluate extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * @var GoodsEvaluate
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new GoodsEvaluate();
    }

    public function index()
    {
        $page = $this->request->get('page/d', 1);
        $page_size = $this->request->get('page_size/d', 10);
        $goods_id = $this->request->get('goods_id/d', 0);

        $condition = [
            'goods_id' => $goods_id,
            'status' => $this->model::STATUS_NORMAL
        ];

        $list = $this->model
            ->where($condition)
            ->field('id,nickname,avatar,scores,content,images,is_anonymous,goods_attr,createtime')
            ->order('createtime', 'desc')
            ->paginate(['list_rows' => $page_size, 'page' => $page]);

        if ($list->items()) {
            foreach ($list as $key => $value) {
                $value['nickname'] = $value['is_anonymous'] ? __('Anonymous buyer') : $value['nickname'];

                if ($value['is_anonymous'] == $this->model::IS_ANONYMOUS_YES) {
                    $value['avatar'] = cdnurl('/assets/img/avatar.png', true);
                }

                $value['content'] = $value['content'] ?: __('This user did not fill in the text');

                $value->hidden(['is_anonymous']);
            }
        }

        $this->success('', $list);
    }

    public function add()
    {
        $post = $this->request->post();

        if (empty($post)) {
            $this->error(__('Invalid parameters'));
        }

        $orderLogic = new \app\common\logic\mall\Order();

        $result = $orderLogic->evaluateSubmit($post, $this->auth->getUserinfo());

        if (!$result) {
            $this->error($orderLogic->getError());
        }

        $this->success(__('Operation completed'));
    }
}


